package hu.issue.web;

import hu.issue.ejb.ProjectService;
import hu.issue.ejb.TaskService;
import hu.issue.entity.Project;
import hu.issue.entity.Task;
import hu.issue.exceptions.ProjectNotFoundException;
import hu.issue.framework.web.Constants;
import hu.issue.framework.web.controller.Controller;
import hu.issue.framework.web.controller.Inject;
import hu.issue.framework.web.controller.RequiredRole;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequiredRole(Constants.ROLE_USER)
public class ProjectController implements Controller {
	@Inject
	private ProjectService projectService;

	@Inject
	private TaskService taskService;

	@Override
	public String handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
		int id;
		try {
			id = Integer.parseInt(request.getParameter("projectId"));
		} catch (final Exception ex) {
			id = 0;
		}

		try {
			final Project project = this.projectService.findProjectById(id);
			request.setAttribute("project", project);
			final List<Task> tasks = this.taskService.getInactiveTasks();
			request.setAttribute("inactiveTasks", tasks);
		} catch (final ProjectNotFoundException ex) {
			return "project-not-found.jsp";
		}

		return "project.jsp";
	}
}
